package com.healthtop.utils;


import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

import java.io.ByteArrayOutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * * 椿熙堂信息研发部
 * Author: 道长 on 2016/10/13.
 */
public class QrcodeUtils {

    public static ByteArrayOutputStream buildQrcode(int width, int height, String filename, String memberCardNo) {
        try {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            String format = "png";// 图像类型
            Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
            hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
            BitMatrix bitMatrix = new MultiFormatWriter().encode(memberCardNo, BarcodeFormat.QR_CODE, width, height, hints);// 生成矩阵
            MatrixToImageWriter.writeToStream(bitMatrix, format, bos);// 输出图像
            return bos;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }
}
